// common/pet_category_picker/pet_category_picker.js
const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js')
var index = [0, 0]

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showCategory:{    //控制area_select显示隐藏
      type:Boolean,
      value:false
    },
    maskShowCategory:{    //是否显示蒙层
      type: Boolean,
      value: true
    }
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    categoryParents: {},
    categoryChilds: {},
    selectedCategoryParent: '',
    selectedCategoryChild: '',
    categoryValue: [0, 0]
  },
  //组件生命周期函数，在组件布局完成后执行
  ready:function(){
    
    var that = this;
    //发起网络请求
    http.post(appConfig.baseUrl + 'Notice/GetCategoryListByParentId',
      { ParentId: 0 }, function (res) {
        //console.log(res.data);
        if (res && res.data && res.data.length > 0) {
          that.setData({
            categoryParents: res.data,
            categoryValue: [0, 0],
            selectedCategoryParent: res.data[0].Name
          });
          http.post(appConfig.baseUrl + 'Notice/GetCategoryListByParentId',
            { ParentId: that.data.categoryParents[0].Id }, function (res) {
              //console.log(res.data);
              if (res && res.data && res.data.length > 0) {
                that.setData({
                  categoryChilds: res.data,
                  selectedCategoryChild: res.data[0].Name
                });

              }
            });
        }
      });
    
    
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handlepetCategoryChange:function(e){
      var that = this;
      console.log("e:" + JSON.stringify(e));
      var value = e.detail.value;
      
      
      /**
       * 滚动的是省
       * 省改变 市、区都不变
       */
      if(index[0] != value[0]){
        index = [value[0],0]
        //发起网络请求
        http.post(appConfig.baseUrl + 'Notice/GetCategoryListByParentId',
          { ParentId: that.data.categoryParents[value[0]].Id }, function (res) {
          console.log(res.data);
          if(res && res.data && res.data.length>0){
            that.setData({
              categoryChilds: res.data,
              selectedCategoryChild: that.data.categoryChilds[0].Name,
              categoryValue: [index[0], 0],
              selectedCategoryParent: res.data[value[0]].Name
            });
          }
        });

      } else if (index[1] != value[1]){
        /**
         * 市改变了 省不变 区变
         */
        index = [value[0], value[1]]
        that.setData({
          categoryValue: [index[0], index[1]],
          categoryParents: that.data.categoryParents,
          selectedCategoryParent: that.data.categoryParents[value[0]].Name,
          categoryChilds: that.data.categoryChilds,
          selectedCategoryChild: that.data.categoryChilds[value[1]].Name
        });


        
      }
    },
    /**
     * 确定按钮的点击事件
     */
    handlepetCategorySelect:function(e){
      //console.log("e:" + JSON.stringify(e));
      var myEventDetail = e; // detail对象，提供给事件监听函数
      var myEventOption = {}; // 触发事件的选项
      this.triggerEvent('sureSelectCategory', myEventDetail, myEventOption)
    },
    /**
     * 取消按钮的点击事件
     */
    handlepetCategoryCancle:function(e){
      var that = this;
      console.log("e:" + JSON.stringify(e))
      that.setData({
        showCategory:false
      });
      this.triggerEvent('canncelSelectArea', e, {})
    },
    pickerCancel:function(e)
    {
      var that = this;
      if (that.data.showCategory==true)
      {
        this.handlepetCategoryCancle();
      }
    }
  }
})
